package com.demo.java.OD151_200.OD163;

import java.util.*;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 【通信误码】问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/145777151
 */
public class OdMain {
    public static void main(String[] args) {
        try (Scanner s = new Scanner(System.in)) {
            int n = Integer.parseInt(s.nextLine());
            String line = s.nextLine();
            int res = solveMethod(n, line);
            System.out.println(res);
        }
    }

    private static int solveMethod(int n, String line) {
        int len = Integer.MAX_VALUE;
        if (n == 0) {
            return 0;
        }
        // 将输入的数字按空格拆分并转成数组
        String[] split = line.split(" ");
        int[] ints = new int[split.length];
        int maxCount = 0;
        Set<Integer> maxCode = new HashSet<>();
        Map<Integer, Integer> codeCount = new HashMap<>();
        Map<Integer, Integer> codeMinIndex = new HashMap<>();
        Map<Integer, Integer> codeMaxIndex = new HashMap<>();

        // 遍历所有数字
        for (int i = 0; i < split.length; ++i) {
            int code = Integer.parseInt(split[i]);
            ints[i] = code;
            int count = codeCount.getOrDefault(code, 0) + 1;
            maxCount = Math.max(maxCount, count);
            codeCount.put(code, count);

            // 记录该数字的首次和最后一次出现的索引
            if (!codeMinIndex.containsKey(code)) {
                codeMinIndex.put(code, i);
            }
            codeMaxIndex.put(code, i);
        }

        // 找出出现次数最多的数字
        for (Map.Entry<Integer, Integer> entry : codeCount.entrySet()) {
            if (entry.getValue() == maxCount) {
                maxCode.add(entry.getKey());
            }
        }

        // 对于每个出现次数最多的数字，计算最小区间长度
        for (Integer i : maxCode) {
            int lo = codeMinIndex.get(i);
            int hi = codeMaxIndex.get(i);
            len = Math.min(len, hi - lo + 1);
        }
        return len;
    }
}